<?php
class NaplokController extends AppController {

	var $name = 'Naplok';
	var $uses = array('Naplo', 'Osztaly', 'Szamla');

	function isAuthorized(){
		if($this->Auth->user('csoport') == 1){		//admin jogosultságú júzerek
			return true;
		}
		if($this->Auth->user('csoport') == 2){		//admin jogosultságú júzerek
			return true;
		}
		return false;		//minden más tiltva
	}

	function index() {
		$this->Naplo->recursive = 0;
		$this->set('naplok', $this->paginate('Naplo', array('Naplo.ev' => $this->Session->read('Settings.ev'), 'Naplo.ceg_id' => $this->Session->read('Settings.cegId'))));
	}

	function mutat($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Naplo.'), 'default', array('class' => 'alert'));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('naplo', $this->Naplo->read(null, $id));
	}

	function uj() {
		if (!empty($this->request->data)) {
			$this->Naplo->create();
			$this->request->data['Naplo']['alapertelmezett_osztaly'] = $this->request->data['Naplo']['alapertelmezett_osztaly'] ? $this->request->data['Naplo']['alapertelmezett_osztaly'] : 0;
			if ($this->Naplo->save($this->request->data)) {
				$this->Session->setFlash(__('The Naplo has been saved'));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Naplo could not be saved. Please, try again.'), 'default', array('class' => 'alert'));
			}
		}
		$naplotipusok = $this->Naplo->Naplotipus->find('list', array('fields' => 'Naplotipus.nev'));
		$valutak = $this->Naplo->Valuta->find('list', array('fields' => 'Valuta.nev'));
		$osztalyok = $this->Osztaly->find('list', array('fields' => 'Osztaly.nev'));
		$szamlak = $this->Szamla->find(
											'all',
											array(
												'conditions' => array(
																		'ceg_id' => $this->Session->read('Settings.cegId'),
																		'ev' => array(0, $this->Session->read('Settings.ev'))
																		),
												'fields' => array('Szamla.id', 'Szamla.szam', 'Szamla.nev'),
												'recursive' => -1
												)
											);
		//debug($szamlak);
		$this->set(compact('valutak', 'naplotipusok', 'osztalyok', 'szamlak'));
	}

	function szerkeszt($id = null) {
		if (!$id && empty($this->request->data)) {
			$this->Session->setFlash(__('Invalid Naplo'), 'default', array('class' => 'alert'));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->request->data)) {
			$this->request->data['Naplo']['ceg_id'] = $this->Session->read('Settings.cegId');
			if ($this->Naplo->save($this->request->data)) {
				$this->Session->setFlash(__('The Naplo has been saved'));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Naplo could not be saved. Please, try again.'), 'default', array('class' => 'alert'));
			}
		}
		if (empty($this->request->data)) {
			$this->Naplo->unBindModel(array('hasMany' => array('Bizonylat')));
			$this->request->data = $this->Naplo->read(null, $id);
		}
		$valutak = $this->Naplo->Valuta->find('list');
		$naplotipusok = $this->Naplo->Naplotipus->find('list');
		$this->set(compact('valutak','naplotipusok'));
	}
}
?>